package com.company.project.controller;

import com.company.project.common.aop.annotation.LogAnnotation;
import com.company.project.common.utils.DataResult;
import com.company.project.entity.SysUser;
import com.company.project.service.HttpSessionService;
import com.company.project.service.UserRoleService;
import com.company.project.service.UserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.wf.captcha.utils.CaptchaUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author ZhangYu
 * @version 0.1.0
 * @create 2020-12-01 15:13
 * @since 0.1.0
 **/
@RestController
@Api(tags = "登录管理")
@RequestMapping("/")
public class LoginController {

    @Resource
    private UserService userService;
    @Resource
    private UserRoleService userRoleService;
    @Resource
    private HttpSessionService httpSessionService;

    @PostMapping(value = "/user/login")
    @ApiOperation(value = "用户登录接口")
    @ApiOperationSupport(includeParameters = {"vo.username","vo.password"})
    public DataResult login(@RequestBody @Valid SysUser vo, HttpServletRequest request) {
//        //判断验证码
//        if (!CaptchaUtil.ver(vo.getCaptcha(), request)) {
//            // 清除session中的验证码
//            CaptchaUtil.clear(request);
//            return DataResult.fail("验证码错误！");
//        }
        return DataResult.success(userService.login(vo));
    }

    @PostMapping(value = "/user/test")
    @ApiOperation(value = "测试接口")
    public DataResult<SysUser> test() {
        SysUser user = new SysUser();
        user.setUsername("adada");
        user.setPassword("adada");
        return DataResult.success(user);
    }

    @PutMapping("/user/info")
    @ApiOperation(value = "更新自己账号信息接口")
    @LogAnnotation(title = "用户管理", action = "更新用户信息")
    public DataResult updateUserInfoById(@RequestBody SysUser vo) {
        userService.updateUserInfoMy(vo);
        return DataResult.success();
    }

    @GetMapping("/user")
    @ApiOperation(value = "查询自己账号详情接口")
    @LogAnnotation(title = "用户管理", action = "查询用户详情")
    public DataResult youSelfInfo() {
        String userId = httpSessionService.getCurrentUserId();
        return DataResult.success(userService.getById(userId));
    }

    @GetMapping("/user/logout")
    @ApiOperation(value = "退出登录接口")
    @LogAnnotation(title = "用户管理", action = "退出")
    public DataResult logout() {
        httpSessionService.abortUserByToken();
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return DataResult.success();
    }

    @PutMapping("/user/pwd")
    @ApiOperation(value = "修改密码接口")
    @LogAnnotation(title = "用户管理", action = "更新密码")
    public DataResult updatePwd(@RequestBody SysUser vo) {
        if (StringUtils.isEmpty(vo.getOldPwd()) || StringUtils.isEmpty(vo.getNewPwd())) {
            return DataResult.fail("旧密码与新密码不能为空");
        }
        String userId = httpSessionService.getCurrentUserId();
        vo.setId(userId);
        userService.updatePwd(vo);
        return DataResult.success();
    }

}
